package com.lixin.smartclassroom.ui.home.tab1.activity

import android.graphics.Color
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.bannerlist
import com.lixin.smartclassroom.api.coursedetail
import com.lixin.smartclassroom.api.coursepllist
import com.lixin.smartclassroom.api.jingcaidetail
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.ActivityExcitingDetailBinding
import com.lixin.smartclassroom.ui.home.adapter.ExcitingImageBannerAdapter
import com.lixin.smartclassroom.ui.home.adapter.ImageBannerAdapter
import com.lixin.smartclassroom.ui.home.adapter.comment
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description 精彩详情
 * Author: lwc
 * Date:2023/12/11 16:54
 */
class ExcitingMomentDetailAct : BaseActivity<ActivityExcitingDetailBinding>() {
    override fun getViewBinding() = ActivityExcitingDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "精彩详情"
    private var excitingId = ""
    private lateinit var baseBean: BaseBean
    private var listBeanVideo: ArrayList<String>? = null
    private var listBeanImage: ArrayList<String>? = null
    private var isPlaying = false

    override fun iniView() {
        listBeanVideo = ArrayList()
        listBeanImage = ArrayList()
        excitingId = getIntentString("jingcaiid")
        binding.apply {
            tvDescribe.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(7f).toInt())

            scopeNetLife {
                baseBean = jingcaidetail(excitingId)
                binding.apply {
                    tvTitle.text = baseBean.title
                    tvLookNum.text = baseBean.seecount + "人浏览"
                    tvTime.text =  baseBean.createdate
                    tvDescribe.setText(
                        baseBean.content
                    )
                    tvDescribe.setMaxLine(4)

                    if (baseBean.url?.isNotEmpty() == true && baseBean.url?.contains("|") == true) {
                        for (i in 0 until baseBean.url!!
                            ?.split("|")?.size!!) {
                            if (baseBean.url!!.split("|")!![i].contains(".mp4")
                            ) {
                                listBeanVideo!!.add(
                                    baseBean.url!!.split("|")!![i]
                                )
                            } else {
                                listBeanImage!!.add(
                                    baseBean.url!!.split("|")!![i]
                                )
                            }
                        }
                    } else {
                        if (baseBean.url?.contains(".mp4") == true) {
                            listBeanVideo!!.add(baseBean.url!!)
                        } else {
                            listBeanImage!!.add(baseBean.url!!)
                        }
                    }
                    if (listBeanVideo != null && listBeanVideo?.size!! > 0) {
                        btnVideo.isSelected = true
                        btnImage.isSelected = false
                        btnVideo.setBackgroundColor(Color.parseColor("#4B6AEF"))
                        btnImage.setBackgroundColor(Color.parseColor("#999999"))
                        videoPlayer.visible()
                        banner.gone()
                        initVideoPlayer()
                    } else if (listBeanImage != null && listBeanImage?.size!! > 0) {
                        btnVideo.isSelected = false
                        btnImage.isSelected = true
                        btnImage.setBackgroundColor(Color.parseColor("#4B6AEF"))
                        btnVideo.setBackgroundColor(Color.parseColor("#999999"))
                        videoPlayer.gone()
                        banner.visible()
                        initBanner()
                    }
                }
            }
        }
    }

    private fun initVideoPlayer() {
        binding.apply {
            videoPlayer.setUp(listBeanVideo?.get(0), true, "")
            //增加封面
            val imageView = ImageView(this@ExcitingMomentDetailAct)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            Glide.with(this@ExcitingMomentDetailAct).load(baseBean.url).into(imageView)
            videoPlayer.thumbImageView = imageView
            videoPlayer.backButton.gone()
            videoPlayer.fullscreenButton.visible()
            //不需要屏幕旋转
            videoPlayer.isNeedOrientationUtils = false
        }

    }

    private fun initBanner() {
        val adapter = ExcitingImageBannerAdapter(this@ExcitingMomentDetailAct, listBeanImage)
        binding.banner.setAdapter(adapter)
        binding.banner.addBannerLifecycleObserver(this@ExcitingMomentDetailAct)
        binding.banner.scrollTime = 2000
        binding.banner.start()
        adapter.setOnBannerListener { _, position ->

        }
    }


    override fun onClick() {
        binding.apply {
            videoPlayer.onClick {
                if (isPlaying)
                    videoPlayer.onVideoPause()
                else
                    videoPlayer.startPlayLogic()
            }
            videoPlayer.fullscreenButton.onClick {
                start(MediaPlayActivity::class.java) {
                    putExtra("video_url", listBeanVideo?.get(0))
                }
            }
            btnVideo.onClick {
                btnVideo.isSelected = true
                btnImage.isSelected = false
                btnVideo.setBackgroundColor(Color.parseColor("#4B6AEF"))
                btnImage.setBackgroundColor(Color.parseColor("#999999"))
                videoPlayer.visible()
                banner.gone()
                if (listBeanVideo != null && listBeanVideo!!.size > 0)
                    initVideoPlayer()
            }
            btnImage.onClick {
                btnVideo.isSelected = false
                btnImage.isSelected = true
                btnImage.setBackgroundColor(Color.parseColor("#4B6AEF"))
                btnVideo.setBackgroundColor(Color.parseColor("#999999"))
                videoPlayer.gone()
                banner.visible()
                initBanner()
            }


        }
    }

}